﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] != null)
        {
            BindToCart();
        }
        else
        {
            Response.Redirect("Default.aspx");
        }
    }
    private bool CheckQuantity(int id, int newQuant)
    {
        ChoDienTuDataContext cdt = new ChoDienTuDataContext();
        var pd = cdt.Select_ProductByID(id).First();
        if (newQuant > pd.Quantity)
            return false;
        return true;
    }
    private void BindToCart()
    {
        MyCart mc = (MyCart)Session["Cart"];
        if (Session["cart"] == null)
        {
            Label5.Text = "0 items";
            Label4.Text = "0 VND";
            btnCheckOut.Enabled = false;
        }
        else
        {
            gvCart.DataSource = mc.Items;
            gvCart.DataBind();

            Label5.Text = mc.GetNumberofItem().ToString() + " items";
            Label4.Text = mc.GetTotalPrice().ToString() + "VND";
            btnCheckOut.Enabled = true;
        }
    }
    private bool Check(string quant)
    {
        int i;
        bool temp;
        temp = int.TryParse(quant, out i);
        if (!temp)
            return false;
        if (i < 0)
            return false;
        return true;
    }

    protected void btnCheckOut_Click(object sender, EventArgs e)
    {
        Response.Redirect("CheckOut.aspx");
    }

    protected void btnCont_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void gvCart_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvCart.EditIndex = e.NewEditIndex;
        BindToCart();

    }
    protected void gvCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        MyCart cart = (MyCart)Session["Cart"];
        int index = int.Parse(gvCart.Rows[e.RowIndex].Cells[0].Text);
        cart.RemoveItem(index);
        BindToCart();
    }
    protected void gvCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        MyCart cart = (MyCart)Session["Cart"];
        string i = gvCart.Rows[e.RowIndex].Cells[0].Text;
        int id = int.Parse(i);
        string q = ((TextBox)gvCart.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        if (Check(q))
        {
            int quant = int.Parse(q);
            if (CheckQuantity(id, quant))
            {
                cart.UpdateItem(id, quant);
            }
            else
            {
                Response.Write("<script language=javascript>alert('Sorry...You add to much')</script>");
            }
        }
        else
        {
            Response.Write("<script language=javascript>alert('Invalid quantity')</script>");
        }
        gvCart.EditIndex = -1;
        BindToCart();
    }
}